//
//  MatchObj.cpp
//  MathSpeed
//
//  Created by ToanTM on 5/17/14.
//
//

#include "MathObj.h"
#include "GameConfig.h"

bool MathObj::init(){
    paramLeft = 0;
    paramRight = 0;
    result = 0;
    isRight = false;
    
    resetValue();
    return true;
}

MathObj* MathObj::create(GameMode::GamePlayMode gameMode){
    MathObj* ret = new MathObj();
    ret->gameMode = gameMode;
    ret->init();
    ret->autorelease();
    return ret;
}

void MathObj::resetValue(){
    switch (this->gameMode) {
        case GameMode::HARD:
            resetValueForHardMode();
            break;
        case GameMode::NORMAL:
            resetValueForNormalMode();
            break;
        case GameMode::EASY:
            resetValueForEasyMode();
            break;
        default:
            resetValueForEasyMode();
            break;
    }
}

void MathObj::resetValueForEasyMode(){
    CCLOG("resetValueForEasyMode");
    matchOperator = ADDITION;
    paramLeft = arc4random() % 9 + 1;
    int distance = arc4random() % DISTANCE_RESULT;
    if ((arc4random() % 2) == 0) {
        distance = distance * (-1);
    }
    paramRight = arc4random() % 9 + 1;
    result = paramLeft + paramRight + distance;
    if (distance == 0) {
        isRight = true;
    }else{
        isRight = false;
    }
}

void MathObj::resetValueForNormalMode(){
    CCLOG("resetValueForNormalMode");
    int distance = arc4random() % DISTANCE_RESULT;
    matchOperator = SUBTRACTION;
    paramRight = arc4random() % 9 + 1;
        
    if ((arc4random() % 1) == 0) {
        distance = distance * (-1);
    }
    result = arc4random() % 9 + 1;
    paramLeft = paramRight + result + distance;
    
    if (distance == 0) {
        isRight = true;
    }else{
        isRight = false;
    }
}

void MathObj::resetValueForHardMode(){
    CCLOG("resetValueForHardMode");
    if (arc4random() % 2 == 0) {
        resetValueForEasyMode();
    } else {
        resetValueForNormalMode();
    }
}

const char* MathObj::getStringMatchOperator(){
    std::string ret = "";
    switch (this->matchOperator) {
        case ADDITION:
            ret = "+";
            break;
        case SUBTRACTION:
            ret = "-";
            break;
        case MULTIPLICATION:
            ret = "x";
            break;
        case DIVISION:
            ret = "/";
            break;
        default:
            break;
    }
    return ret.c_str();
}